﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using coliseu.BussinessLayer;

namespace coliseu.Views.Rotinas_Diarias
{
    public partial class FrmCadReserva : Form
    {
        private ReservaBL rBL;
        private reserva r;

        private BlocoBL bbl = new BlocoBL();
        private ApartamentoBL abl = new ApartamentoBL();
        private Morador_ApartamentoBL mbl = new Morador_ApartamentoBL();
        private LazerBL lbl = new LazerBL();

        public FrmCadReserva(ReservaBL rBL)
        {
            InitializeComponent();
            this.rBL = rBL;
            reservaBindingSource.AddNew();
        }

        public FrmCadReserva(reserva r, ReservaBL rBL)
        {
            InitializeComponent();
            this.rBL = rBL;
            this.r = r;
            reservaBindingSource.DataSource = r;
        }

        private void hora_inicioLabel_Click(object sender, EventArgs e)
        {


        }

        private void data_utilizacaoLabel_Click(object sender, EventArgs e)
        {

        }

        private void data_utilizacaoDateTimePicker_ValueChanged(object sender, EventArgs e)
        {

        }

        private void hora_fimLabel_Click(object sender, EventArgs e)
        {

        }

        private void FrmCadReserva_Load(object sender, EventArgs e)
        {
            blocoBindingSource.DataSource = bbl.selecionar();

            apartamentoBindingSource.DataSource = abl.selecionar();
            moradorBindingSource.DataSource = mbl.selecionar_apto(Convert.ToInt32(comboBox1.SelectedValue));
            arealazerBindingSource.DataSource = lbl.selecionar();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Validate();
            ValidateChildren();

            ((reserva)reservaBindingSource.Current).data_pedido = data_pedidoDateTimePicker.Value.Date;
            ((reserva)reservaBindingSource.Current).data_utilizacao = data_utilizacaoDateTimePicker.Value.Date;
            ((reserva)reservaBindingSource.Current).id_apartamento = ((apartamento)apartamentoBindingSource.Current).id_apartamento;
            ((reserva)reservaBindingSource.Current).id_morador = ((morador)moradorBindingSource.Current).id_morador;
            ((reserva)reservaBindingSource.Current).id_area_lazer = ((area_lazer)arealazerBindingSource.Current).id_area_lazer;

            reservaBindingSource.EndEdit();

            if (r == null)
                rBL.salvar((reserva)reservaBindingSource.Current, "insert");
            else
                rBL.salvar((reserva)reservaBindingSource.Current, "edit");

            MessageBox.Show("Salvo com sucesso", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Close();
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                apartamentoBindingSource.DataSource = abl.selecionar(comboBox4.Text);
                moradorBindingSource.DataSource = mbl.selecionar_apto(Convert.ToInt32(comboBox1.SelectedValue));
            }
            catch
            {
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                moradorBindingSource.DataSource = mbl.selecionar_apto(Convert.ToInt32(comboBox1.SelectedValue));
            }
            catch
            {
            }
            // comboBox2.ValueMember = "id_morador";
            //  comboBox2.DisplayMember = "nome";            
        }
    }
}
